//
//  OnlineRepertoireViewController.m
//  XiXi
//
//  Created by HawkingWang on 16/9/29.
//  Copyright © 2016年 xi-xi.cc. All rights reserved.
//

#import "OnlineRepertoireViewController.h"

#import "OnlineTableViewCell.h"//cell
#import "SearchView.h"//搜索View

#import "OnlineDetailViewController.h"//在线剧目详情Controller

@interface OnlineRepertoireViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation OnlineRepertoireViewController
{
    NSMutableArray *_operaArray;//剧目Array
    
    SearchView *_searchView;//搜索View
}
 static NSString *const OnlineTableViewCellID = @"OnlineTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置导航栏
    [self setNav];
    
    //注册TableView
    [self registerTableView];
    
    [SVProgressHUD show];
    //获取剧目列表
    [self requestOperaQuery];
    
    //剧目检票时间
    [self requestCheckTime];
    
    //下拉刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestOperaQuery];
        [self requestCheckTime];
    }];
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    
//    [SVProgressHUD show];
//    //获取剧目列表
//    [self requestOperaQuery];
//    
//    //剧目检票时间
//    [self requestCheckTime];
//}

- (void)setNav{
    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(searchRepertoire)];
    self.navigationItem.rightBarButtonItem = search;
}

#pragma mark === 搜索View ===
- (void)searchRepertoire{
    _searchView = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [[UIApplication sharedApplication].keyWindow addSubview:_searchView];
    [_searchView.searchBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    __weak __typeof__ (self)weakSelf = self;
    _searchView.jumpToDetail = ^(){
        [weakSelf close];
//        [weakSelf jumpToDetailWithModel:<#(OnlineModel *)#>];
    };
}
- (void)close{
    [_searchView removeFromSuperview];
}


#pragma mark === 跳转到剧目详情界面 ===
- (void)jumpToDetailWithModel:(OnlineModel *)model{
    OnlineDetailViewController *detail = [[OnlineDetailViewController alloc] init];
    detail.model = model;
    [self presentViewController:detail animated:YES completion:nil];
}


- (void)registerTableView{
    [self.tableView registerNib:[UINib nibWithNibName:@"OnlineTableViewCell" bundle:nil] forCellReuseIdentifier:OnlineTableViewCellID];
}

#pragma mark === TableViewDelegate ===
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _operaArray.count;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OnlineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OnlineTableViewCellID];
    [cell loadData:_operaArray[indexPath.row]];
    cell.image.backgroundColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self jumpToDetailWithModel:_operaArray[indexPath.row]];
}


#pragma mark === 获取在线剧目列表 ===
- (void)requestOperaQuery{
    
    [NetWorkManager requestWithType:0 withMethodName:OperaQuery withParaments:nil withSuccessBlock:^(NSDictionary *object) {
//        DLog(@"%@", object);
        if (!_operaArray) {
            _operaArray = [NSMutableArray array];
        }else [_operaArray removeAllObjects];
        
        for (NSDictionary *dic in object) {
            OnlineModel *model = [OnlineModel initWithDic:dic];
            [_operaArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    } withFailureBlock:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

    } progress:nil];
}

#pragma mark === 获取剧目检票时间 ===
- (void)requestCheckTime{
    [NetWorkManager requestWithType:0 withMethodName:OperaStop withParaments:nil withSuccessBlock:^(NSDictionary *object) {
        
        if (object[@"stop-sale"]) {
            [[NSUserDefaults standardUserDefaults] setObject:object[@"stop-sale"] forKey:StopSale];
        }
        
    } withFailureBlock:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

    } progress:nil];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,4, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 68)style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
@end
